/** @file odpapi.h
 *  @brief Open Data Protection API Helper Header.
 *
 *  This file containers the prototypes and enumerations used by 
 *  The Open Data Protection Helper functions.
 *
 *  @author Gerard J. Nicol (gerard.nicol@gazillabyte.com)
 *  @bug No known bugs.
 */
#pragma once

#include <stdlib.h>
#include <cJSON.h>

enum ODPAPI_COMMONERROR {
	ODPAPI_COMMONERROR_OK                      = 0,
	ODPAPI_COMMONERROR_PARSEFAILED             = 1,
	ODPAPI_COMMONERROR_NOTOFFERED              = 2,
	ODPAPI_COMMONERROR_NOTPERMITTED            = 3,
	ODPAPI_COMMONERROR_FAILEDAUTH              = 4,
	ODPAPI_COMMONERROR_ERRORCOMEBACK           = 5,
	ODPAPI_COMMONERROR_EXCEEDEDALLOWANCE       = 6,
	ODPAPI_COMMONERROR_INVALIDUUID             = 7,
	ODPAPI_COMMONERROR_MISSINGARGUMENT         = 8,
	ODPAPI_COMMONERROR_MISSINGDATA             = 9,
//*
//*
	ODPAPI_COMMONERROR_SOURCELIBRARYNOTKNOWN   = 100,
	ODPAPI_COMMONERROR_TARGETLIBRARYNOTKNOWN   = 101,
	ODPAPI_COMMONERROR_TARGETLIBRARYSAME       = 102,
	ODPAPI_COMMONERROR_LIBRARYCOMPATPROBLEM    = 103,
//*
//*
	ODPAPI_COMMONERROR_UNKNOWN                 = 1024,
};

enum ODPAPI_INVENTORYSTATUS {
	ODPAPI_INVENTORYSTATUS_OK                  = 0,
	ODPAPI_INVENTORYSTATUS_MOVINGIN            = 1,
	ODPAPI_INVENTORYSTATUS_MOVINGOUT           = 2,
	ODPAPI_INVENTORYSTATUS_NOTPRESENT          = 3,
};

enum ODPAPI_MOVESTATUS {
	ODPAPI_MOVESTATUS_OK                       = 0,
	ODPAPI_MOVESTATUS_UNKNOWN                  = 1,
	ODPAPI_MOVESTATUS_NOTINLIBRARY             = 2,
	ODPAPI_MOVESTATUS_DATAERROR                = 3,
	ODPAPI_MOVESTATUS_FAILED                   = 4,
	ODPAPI_MOVESTATUS_MOVENOTREQUIRED          = 5,
	ODPAPI_MOVESTATUS_INVALIDVALUE             = 6,
};

enum ODPAPI_SCANSTATUS {
	ODPAPI_SCANSTATUS_OK                       = 0,
	ODPAPI_SCANSTATUS_DATAERROR                = 1,
	ODPAPI_SCANSTATUS_UNKNOWN                  = 2,
	ODPAPI_SCANSTATUS_FAILED                   = 3,
	ODPAPI_SCANSTATUS_INVALIDVALUE             = 4,
};

enum ODPAPI_ADDSTATUS {
	ODPAPI_ADDSTATUS_OK                       = 0,
	ODPAPI_ADDSTATUS_DATAERROR                = 1,
	ODPAPI_ADDSTATUS_EXISTS                   = 2,
	ODPAPI_ADDSTATUS_EXISTSOTHERLIBRARY       = 3,
	ODPAPI_ADDSTATUS_FAILED                   = 4,
	ODPAPI_ADDSTATUS_INVALIDVALUE             = 5,
};

enum ODPAPI_SCANTYPE {
	ODPAPI_SCANTYPE_UNDEFINED                 = 0,
	ODPAPI_SCANTYPE_IN                        = 1,
	ODPAPI_SCANTYPE_OUT                       = 2,
	ODPAPI_SCANTYPE_AUDIT                     = 3,
};

/** @brief Returns a text representation for a given common error enumeration.
 *
 *  It is recommended that you provide at least 256 bytes of storage for the result area.
 *  This function will also return the address of the storage area as a result so that it can 
 *  be used inline in other fucntions, such as fprintf.
 *
 *  @param buffer The address of the storage buffer to receive the result
 *  @param length The length of the storage buffer to receive the result
 *  @param error  The Open Data Protection API common error code value
 *  @return The address of the storage area
 */
char* odpapi_format_common_result_text(char* buffer,                        //!< pointer to storage for result
	                                   size_t length,                       //!< size of storage for result
									   enum ODPAPI_COMMONERROR error);      //!< common error value

/** @brief Initializes cJSON control area with meta and response sections.
 *
 *  @param root The cJSON root pre-initialized with cJSON_CreateObject()
 *  @param adaptername A string representing the name of this ODPAPI adapter 
 */
cJSON* odpapi_response_prolog(cJSON *root,                                  //!< pointer to cJSON root
							  char* adaptername);                           //!< adapter name

/** @brief Sets the response section with an integer and string representation of the ODPAPI function result.
 *
 *  This function will look for an existing response section (created by odpapi_response_prolog) and set the
 *  result and result_text field to the value passed as the error argument.
 *  If a response section can not be found, one will be created.
 *
 *  @param root The cJSON root
 *  @param error The error returned by the ODPAPI function
 *  @param hint A string value giving further information on the error (or NULL)
 */
void odpapi_response_epilog(cJSON *root,                                    //!< pointer to cJSON root
					        enum ODPAPI_COMMONERROR error,                  //!< common error value
							char* hint);                                    //!< error hint text (or NULL)   

/** @brief Locates a cJSON element for a given key.
 *
 *  @param json The cJSON branch for search
 *  @param key The key being searched for
 */
cJSON* odpapi_request_get_value(cJSON *json,                               //!< pointer to cJSON node
								char* key);                                //!< string to search for

/** @brief Finds the request section.
 *
 *  @param root The cJSON root
 */
cJSON* odpapi_request_get_request(cJSON *root);                            //!< pointer to the cJSON request root

/** @brief Creates a Move Response Element.
 *
 *  @param responsearray Pointer to the cJSON response array node
 *  @param status enumeration representing the status of this element
 *  @param sequence index of element (starting at zero)
 *  @param id object id string
 *  @param movedate move_date value in ISO date format
 *  @param hint of any error
 */
void odpapi_move_response_helper(cJSON *responsearray,
							     enum ODPAPI_MOVESTATUS status,
								 int sequence,
								 char* id,
								 char* type,
								 char* movedate,
								 char* hint);

/** @brief Creates an Inventory Response Element.
 *
 *  @param responsearray Pointer to the cJSON response array node
 */
void odpapi_inventory_response_helper(cJSON *responsearray,
							          enum ODPAPI_INVENTORYSTATUS status,
								      int sequence,
								      char* id,
								      char* type,
									  char* description,
									  char* movedate,
									  int encrypted,
									  int scratch,
									  int automatemovedate);

/** @brief Creates a Scan Response Element.
 *
 *  @param responsearray Pointer to the cJSON response array node
 */
void odpapi_scan_response_helper(cJSON *responsearray,
							     enum ODPAPI_SCANSTATUS status,
							     int sequence,
							     char* id,
							     char* type,
							     char* hint);

/** @brief Creates an Add Response Element.
 *
 *  @param responsearray Pointer to the cJSON response array node
 */
void odpapi_add_response_helper(cJSON *responsearray,
							    enum ODPAPI_ADDSTATUS status,
							    int sequence,
								char* id,
							    char* hint);

/** @brief Creates a Library List Response Element.
 *
 *  @param responsearray Pointer to the cJSON response array node
 */
cJSON* odpapi_list_response_helper(cJSON *responsearray,
								   int sequence,
								   char* description,
								   char* guid,
								   unsigned int capacity,
								   unsigned int capacityused,
								   unsigned int movingin,
								   unsigned int movingout,
								   int automatemovedate,
								   char **targetguids);

